package gov.child.web.controller;

import gov.child.entities.vo.Department;
import gov.child.entities.vo.Guide;
import gov.child.service.DepartmentService;
import gov.child.service.GuideService;
import gov.child.util.FileSaver;
import gov.child.util.LocalCode;
import gov.child.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author fzh
 * @since 2017/9/19
 */
@Controller
@RequestMapping("/guide")
public class GuideController {

    @Qualifier("localCode")
    @Autowired
    private LocalCode localCode;

    @Qualifier("fileSaver")
    @Autowired
    private FileSaver saver;

    @Autowired
    private GuideService guideService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 资源操纵
     *
     * @param guide 指南
     * @param file  文件
     * @return 跳转视图
     * @throws IOException 文件写入错误
     */
    @PostMapping("/add")
    public ModelAndView guide(Guide guide, @RequestParam(required = false, value = "file") MultipartFile file, HttpSession session) throws IOException {
        if (file.getSize() == 0) {
            guide.setFlowChart("");
        } else {
            String path = saver.save(file.getOriginalFilename(), file.getBytes());
            guide.setFlowChart(path);
        }
        Department department = (Department) session.getAttribute("dept");
        List l = (List) session.getAttribute("childDepts");
        StringBuilder builder = new StringBuilder(localCode.getCode() + "-");
        builder.append(department.getDeptCode()).append("-");
        for (Object o : l) {
            if (Objects.equals(((Department) o).getDepartmentId(), guide.getDeptC())) {
                builder.append(((Department) o).getDeptCode()).append("-");
                break;
            }
        }
        builder.append(StringUtil.randomString(8));
        guide.setGuideCode(builder.toString());
        guide.setCreateTime(new Date());
        guideService.save(guide);
        return new ModelAndView("forward:/admin/guide/manage?option=all&start=1");
    }

    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam Integer id) {
        Guide guide = guideService.findById(id);
        Department deptP = departmentService.findById(guide.getDeptP());
        Department deptC = departmentService.findById(guide.getDeptC());
        ModelAndView mav = new ModelAndView("guide_detail");
        mav.addObject("guide", guide);
        mav.addObject("deptP", deptP);
        mav.addObject("deptC", deptC);
        return mav;
    }

    @GetMapping("/delete")
    public JSONObject delete(@RequestParam Integer id) {
        return null;
    }
}
